package com.jydd.singlekotlinmvvm.comm.feature

import androidx.lifecycle.viewModelScope
import com.jydd.singlekotlinmvvm.comm.base.BaseLiveData
import com.jydd.singlekotlinmvvm.comm.base.BaseViewModel
import com.jydd.singlekotlinmvvm.comm.base.SingleLiveEvent
import com.jydd.singlekotlinmvvm.comm.model.response.UserInfo
import com.jydd.singlekotlinmvvm.comm.repo.UserDataSource
import kotlinx.coroutines.launch

/**
 *  author: 韦敏敏
 *  date: 2020/1/21 0021
 */
class LauncherViewModel(
    private val userRepo: UserDataSource
) : BaseViewModel() {
    private val _userInfo = BaseLiveData<UserInfo>()
    val userInfo: BaseLiveData<UserInfo>
        get() = _userInfo
    private val _moduleFlag = SingleLiveEvent<HomeModule?>()
    val moduleFlag : SingleLiveEvent<HomeModule?>
    get()= _moduleFlag
    private val _searchKey = SingleLiveEvent<String>()
    val searchKey: SingleLiveEvent<String>
        get() = _searchKey
    fun fetchCurrUser(){
        viewModelScope.launch {
            _userInfo.update(userRepo.currUser())
        }
    }
    fun selectModule(module : HomeModule){
        if (_moduleFlag.value != module){
            _moduleFlag.value = module
        }
    }
    fun queryResult(key: String){
        if (_searchKey.value != key) _searchKey.value = key

    }
    enum class HomeModule(var module: String){
        REPOSITORIES("repos"), STARS("starred")
    }
}